package com.campus.service.impl;

import com.campus.constant.MessageConstant;
import com.campus.constant.StatusConstant;
import com.campus.context.CurrentHolder;
import com.campus.exception.DeletionNotAllowedException;
import com.campus.exception.ResourceNotFoundException;
import com.campus.exception.StudyRoomNameDuplicateException;
import com.campus.mapper.SportsVenueMapper;
import com.campus.pojo.dto.SportsVenueDTO;
import com.campus.pojo.dto.SportsVenuePageQueryDTO;
import com.campus.pojo.entity.SportsVenue;
import com.campus.pojo.vo.SportsVenueVO;
import com.campus.result.PageResult;
import com.campus.service.SportsVenueService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SportsVenueServiceImpl implements SportsVenueService {

    @Autowired
    private SportsVenueMapper sportsVenueMapper;

    /**
     * 新增体育场馆
     *
     * @param sportsVenueDTO 体育场馆数据传输对象
     */
    @Override
    public void save(SportsVenueDTO sportsVenueDTO) {
        // 1.查询数据库中是否有相同名称的
        String name = sportsVenueDTO.getName();
        SportsVenue sportsVenue = sportsVenueMapper.getByName(name);
        if (sportsVenue != null) {
            // 存在相同名称的
            throw new StudyRoomNameDuplicateException(MessageConstant.VENUE_NAME_ALREADY_EXISTS);
        }

        // 2.构建entity对象-library
        sportsVenue = SportsVenue.builder()
                .name(sportsVenueDTO.getName())
                .location(sportsVenueDTO.getLocation())
                .capacity(sportsVenueDTO.getCapacity())
                .status(StatusConstant.STATUS_NORMAL)
                .build();

        //3.调用mapper方法
        sportsVenueMapper.insert(sportsVenue);
    }

    /**
     * 批量删除体育场馆
     *
     * @param ids 体育场馆ID集合
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        //1.是否处于被预约状态
        for (Long id : ids) {
            SportsVenue sportsVenue = sportsVenueMapper.getById(id);
            if (sportsVenue == null) {
                throw new ResourceNotFoundException(MessageConstant.VENUE_NOT_FOUND);
            }
            if (sportsVenue.getStatus() == StatusConstant.STATUS_RESERVED) {
                throw new DeletionNotAllowedException(MessageConstant.VENUE_IS_RESERVED);
            }
        }

        //2.调用mapper方法
        sportsVenueMapper.deleteByIds(ids);
    }

    /**
     * 分页查询体育场馆
     *
     * @param sportsVenuePageQueryDTO 分页查询条件
     * @return PageResult<SportsVenueVO> 分页结果
     */
    @Override
    public PageResult<SportsVenueVO> pageQuery(SportsVenuePageQueryDTO sportsVenuePageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(sportsVenuePageQueryDTO.getCurrentPage(), sportsVenuePageQueryDTO.getPageSize());

        //2.调用mapper层
        Page<SportsVenueVO> page = sportsVenueMapper.pageQuery(sportsVenuePageQueryDTO);

        //3.返回结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 修改体育场馆状态
     *
     * @param id     体育场馆ID
     * @param status 状态值
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        //1.查询体育场馆是否存在
        SportsVenue sportsVenue = sportsVenueMapper.getById(id);
        if (sportsVenue == null) {
            //不存在
            throw new ResourceNotFoundException(MessageConstant.VENUE_NOT_FOUND);
        }

        //2.构建entity对象-SportsVenue
        //如果要改为正常状态即无预约 此时需将userId置为null
        if (status == StatusConstant.STATUS_NORMAL) {
            sportsVenue = SportsVenue.builder()
                    .id(id)
                    .status(status)
                    .userId(null)
                    .build();

        } else {
            sportsVenue = SportsVenue.builder()
                    .id(id)
                    .status(status)
                    .userId(CurrentHolder.getCurrentId())
                    .build();
        }


        //3.调用mapper方法
        sportsVenueMapper.update(sportsVenue);
    }

    /**
     * 根据ID查询体育场馆详情
     *
     * @param id 体育场馆ID
     * @return SportsVenue 体育场馆实体对象
     */
    @Override
    public SportsVenue getById(Long id) {
        return sportsVenueMapper.getById(id);
    }

    /**
     * 修改体育场馆信息
     *
     * @param sportsVenueDTO 体育场馆数据传输对象
     */
    @Override
    public void update(SportsVenueDTO sportsVenueDTO) {


        //1.构建entity对象-SportsVenue 以便update方法复用
//        SportsVenue sportsVenue = SportsVenue.builder()
//                .id(sportsVenueDTO.getId())
//                .name(sportsVenueDTO.getName())
//                .location(sportsVenueDTO.getLocation())
//                .capacity(sportsVenueDTO.getCapacity())
//                .build();

        //属性比较多 采用属性拷贝的方式
        SportsVenue sportsVenue = new SportsVenue();
        BeanUtils.copyProperties(sportsVenueDTO, sportsVenue);

        //2.调用mapper方法
        sportsVenueMapper.update(sportsVenue);
    }
}
